public class Solution {
    public String reverseWords(String s) {
        //去除两端的空格
        s = s.trim();

        String[] words = s.split("\\s+");
        int len = words.length;
        int left = 0, right = len - 1;
        while(left < right){
            String tmp = words[left];
            words[left] = words[right];
            words[right] = tmp;
            left++;
            right--;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < len; i++){
            stringBuilder.append(words[i]);
            if(i < len - 1){
                stringBuilder.append(" ");
            }
        }
        return stringBuilder.toString();

    }

}
